<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\ArticleCate;

use app\common\model\Iplog;
use app\common\model\ThemeAdmin;
use app\common\model\UserLoginLog;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use think\Request;

class AdminAdmin extends AdminInit
{
    public function __construct()
    {

        parent::__construct();
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //今日新增标签
        $tags_today_add_count = Db::name("tags")
            ->whereDay('create_time')
            ->count();
        View::assign("tags_today_add_count","$tags_today_add_count");

        //今日活跃用户
        $today_user = UserLoginLog::whereDay('create_time')
            ->distinct(true)->field('uid')
            ->count();
        View::assign("today_user",$today_user);
        View::assign("title","文章列表");
        return  view();
    }
    public function repassword()
    {

        if(\think\facade\Request::isAjax()){
            $d = \think\facade\Request::param();
            $user = $this->user;
            if( md5($d["old_pass"]) != $user["password"]){
                return json(["code"=>4000,"msg"=>"旧密码错误","data"=>""]);
            }
            if(strlen($d["new_pass"]) < 6){
                return json(["code"=>4000,"msg"=>"新密码长度不能小于6","data"=>""]);
            }
            if($d["new_pass"] != $d["re_new_pass"]){
                return json(["code"=>4000,"msg"=>"两次新密码不一致","data"=>""]);
            }
            $pass = md5($d["new_pass"]);
            $res = \app\common\model\User::where("id",$user["id"])->save(["password"=>$pass]);
            if($res){
                //更新session
                \session("index_user",\app\common\model\User::find($user["id"]));

                return  json(["code"=>200,"msg"=>"成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"请稍后再试"]);
            }
        }
        return  view();
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        View::assign("title","文章发布");
        return \view();
    }

    //ajax 获取分类
    public function get_cate()
    {
        $pid = \think\facade\Request::post("pid");
        $mod = new ArticleCate();
        $list = $mod->where("pid",$pid)->select();
        return json(["code"=>200,"msg"=>"成功","data"=>$list]);
    }

    //默认框架欢迎页
    public function lyear_main()
    {
        //今日访问pv数


        return view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
       
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        $mod = new \app\common\model\Article();
        $d = $request->post();
        $d["status"] = 1;
        $d["uid"] = $user["id"];
        $d["author"] = $user["nickname"];
        $d["userinfo"] = json_encode($user);
        if(isset($d["id"])){
            $res = Db::name("article")->strict(false)->where("id",$d["id"])->update($d);
        }else{
            $res = $mod->save($d);
        }

        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

    public function saveInfo()
    {
        return view();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

        $article = Db::name("article")->find($id);
        Db::name("article")->where("id",$id)
            ->inc("hits")
            ->update();

        View::assign("article",$article);

        if($user = Session::get("index_user")){
            $uid = $user["id"];
            $order = Db::name('article_order')->where(['aid'=>$article['id'],'uid'=>$uid,'status'=>2])->find();
            if($order){

                View::assign('order',$order);
            }else{
                View::assign('order',0);
            }
        }else{

            View::assign('order',0);
        }
        View::assign('title',$article['title']);

        // 上一篇文章和下一篇文章
        $pre_article = Db::name("article")->where("id","<",$id)->order("id DESC")->find();
        $next_article = Db::name("article")->where("id",">",$id)->order("id ASC")->find();

        View::assign('pre_article',$pre_article);

        View::assign('next_article',$next_article);

        //调取评论
        $comment = Db::name("comment")->where("contentid",$id)
            ->json(["userinfo"])
            ->paginate(["query"=>request()->param()]);

        View::assign('comment',$comment);

        return view('read');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {


    }

    public function cache_clear()
    {
        Cache::clear();
    }

    //保存用户的主题
    public function theme_save()
    {
        $d = input("param.");
        $map = $d;
        unset($map["value"]);
        $user = $this->user;
        $map["uid"] = $user["id"];
        $res = ThemeAdmin::where($map)->find();
        if(!$res){
            ThemeAdmin::create($map);
            return json(["code"=>200,"msg"=>"主题新建成功"]);
        }else{
            $res->value = $d["value"];
            $res->save();
            return json(["code"=>200,"msg"=>"主题更新成功"]);
        }
    }

    //用户的主题初始化
    public function user_theme_init()
    {
        $user = $this->user;
        $map["uid"] = $user["id"];
        $res = ThemeAdmin::where($map)->select();
        if($res){
            return json(["code"=>200,"msg"=>"主题更新成功","data"=>$res]);
        }
    }


}
